// token
// var jwt=require('jsonwebtoken')
var jwt = require('koa-jwt')
var {secret} = require('../config')

var router = require('koa-router')()

// 二级嵌套结构的增删改查，一个问题对应多个答案
// 二级嵌套结构的增删改查，答案      问题id     答案     
router.prefix('/questions/:questionId/answers')

const {
    checkAnswerExist,
    checkAnswer,
    findAll,
    find,
    findById,
    create,
    update,
    remove,
    findQAll
} = require('../controllers/answers')

const auth = jwt({secret})

// 查询所有答案
// router.get('/all', findAll)

// 查询特定问题的全部答案
// router.get('/:id/list', findQAll)


// 查询特定问题的答案列表
router.get('/', find)
// 查询特定问题的答案
router.get('/:id', auth, checkAnswerExist, findById)
// 创建答案
router.post('/', auth, create)
// 更新答案
router.patch('/:id', auth, checkAnswerExist, checkAnswer, update)
// 删除答案
router.delete('/:id', auth, checkAnswerExist, checkAnswer, remove)


module.exports = router
